package com.lanxin.oa.zc.service.serviceImpl;

import com.lanxin.bean.ZcAssetbaojia;
import com.lanxin.bean.ZcAssetpurchaseapply;
import com.lanxin.bean.ZcAssetpurchaseapplydetails;
import com.lanxin.oa.zc.dao.ZcGouMaiDao;
import com.lanxin.oa.zc.service.ZcGouMaiService;
import com.lanxin.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class ZcGouMaiServiceImpl implements ZcGouMaiService {
    @Autowired
    ZcGouMaiDao gmdao;

    @Override
    public List<ZcAssetpurchaseapply> selectzcgm(Integer empid) {
        return gmdao.selectzcgm(empid);
    }

    @Override
    public List selectflow() {
        return gmdao.selectflow();
    }

    @Override
    public List selectEmpByempName() {
        return gmdao.selectEmpByempName();
    }

    @Override
    public List selectDeptById() {
        return gmdao.selectDeptById();
    }

    @Override
    public List selectJob() {
        return gmdao.selectJobById();
    }

    @Override
    public void insert(ZcAssetpurchaseapply zcAssetpurchaseapply, String[] assetName, String[] assetNumber, String[] guige, Integer[] shengou, Double[] unitPrice, Integer[] quantity) {
        String dh = this.createFlowId();
        zcAssetpurchaseapply.setDocNo(dh);

        if (assetName != null) {
            for (int i = 0; i < assetName.length; i++) {
                ZcAssetpurchaseapplydetails gmmx = new ZcAssetpurchaseapplydetails();
                gmmx.setAssetName(assetName[i]);
                gmmx.setParentid(zcAssetpurchaseapply.getOid());
                gmmx.setAssetNumber(assetNumber[i]);
                gmmx.setGuige(guige[i]);
                gmmx.setShengou(shengou[i]);
                gmmx.setQuantity(quantity[i]);
                gmmx.setUnitPrice(unitPrice[i]);
                gmmx.setPurchaseDate(new Date());
                zcAssetpurchaseapply.getZcgmmx().add(gmmx);
            }
        }
        //创建日期
        try {
            zcAssetpurchaseapply.setCreateDate(new Date());

        } catch (Exception e) {
            e.printStackTrace();
        }
        zcAssetpurchaseapply.setStatus("W");
        gmdao.add(zcAssetpurchaseapply);


    }

    @Override
    public String createFlowId() {
        Integer tadayFlowCount = gmdao.getZcYdInfoCount();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        StringBuilder taday = new StringBuilder(format.format(new Date()));
        StringBuilder prefix = new StringBuilder("ZCGM");

        if (tadayFlowCount == 0) {
            prefix.append(taday).append("01");
        } else {
            if (tadayFlowCount < 10) {
                prefix.append(taday).append("0").append(tadayFlowCount + 1);
            } else {
                prefix.append(taday).append(tadayFlowCount + 1);
            }
        }

        return prefix.toString();
    }

    @Override
    public void tijiao(String status, Integer oid) {
        ZcAssetpurchaseapply zcgm = gmdao.selectByid(oid);
        zcgm.setSubmitDate(new Date());
        System.out.println("status:"+status);
        if (status.equals("W")) {
            zcgm.setStatus("B");
        }
        gmdao.update(zcgm);
    }

    @Override
    public ZcAssetpurchaseapply selectByid(Integer oid) {
       return (ZcAssetpurchaseapply)gmdao.selectByid(oid);
    }

    @Override
    public void update(ZcAssetpurchaseapply zcAssetpurchaseapply,Integer mxoid[], String[] assetName, String[] assetNumber, String[] guige, Integer[] shengou, Double[] unitPrice, Integer[] quantity) {
        ZcAssetpurchaseapply gm = gmdao.selectByid(zcAssetpurchaseapply.getOid());
        gm.getZcgmmx().clear();
        if (assetName != null) {
            Set<ZcAssetpurchaseapplydetails> mx = new HashSet<>();
            for (int i = 0; i < assetName.length; i++) {
                ZcAssetpurchaseapplydetails gmmx= new ZcAssetpurchaseapplydetails();
                if (mxoid != null && mxoid.length > 0) {
                    if (i < mxoid.length) {
                        gmmx.setOid(mxoid[i]);
                    }
                }

                gmmx.setAssetName(assetName[i]);
                gmmx.setParentid(zcAssetpurchaseapply.getOid());
                gmmx.setAssetNumber(assetNumber[i]);
                gmmx.setGuige(guige[i]);
                gmmx.setShengou(shengou[i]);
                gmmx.setQuantity(quantity[i]);
                gmmx.setUnitPrice(unitPrice[i]);
                gmmx.setPurchaseDate(new Date());
                mx.add(gmmx);
                zcAssetpurchaseapply.getZcgmmx().add(gmmx);
            }
            gm.setZcgmmx(mx);
        }
        gmdao.update(gm);
    }

    @Override
    public boolean delete(ZcAssetpurchaseapply zcgm) {
        List<ZcAssetpurchaseapplydetails> mxlist= gmdao.selectmx(zcgm.getOid());
        if (mxlist!= null && mxlist.size() > 0) {
            return false;
        }
        gmdao.delete(zcgm);
        return  true;
    }

    @Override
    public List selectmx(Integer oid) {
        return gmdao.selectmx(oid);
    }

    @Override
    public void insertbaojia(ZcAssetpurchaseapplydetails gmmx, Double[] price, String[] linkman, String[] linkPhone, String[] remark) {
        ZcAssetpurchaseapply gm = gmdao.selectByid(gmmx.getParentid());
        System.out.println(gmmx.getParentid()+"getParentid");
        if (price!=null){
            ZcAssetbaojia bj=new ZcAssetbaojia();
            for (int i = 0; i < price.length; i++){
                bj.setParentid(gmmx.getOid());
                bj.setPrice(price[i]);
                bj.setLinkman(linkman[i]);
                bj.setLinkPhone(linkPhone[i]);
                bj.setRemark(remark[i]);
                gmdao.add(bj);
            }
        }
        gm.setStatus("S");
        gmdao.update(gm);
    }

    @Override
    public PageBean<ZcAssetpurchaseapply> selectallzcgm(Integer page, Integer pageSize) {
        return gmdao.selectallzcgm(page,pageSize);
    }

}
